<?php

namespace App\HttpController\Admin;

use App\Model\AdminsModel;

class OperateController extends AuthController {

    /**
     * @title 管理人员列表
     * @description 获取管理人员列表
     * @method GET
     * @request manage/operate
     * @param int page 页码 false 1
     * @param string kwd 用户名/用户昵称 false ''
     * @return int id 编号
     * @return string username 用户名
     * @return string nickname 用户昵称
     * @return int role_id 角色ID
     * @return int status 状态
     * @return string status_text 状态注解
     * @return string created_at 添加时间
     * @throws \EasySwoole\Mysqli\Exceptions\ConnectFail
     * @throws \EasySwoole\Mysqli\Exceptions\Option
     * @throws \EasySwoole\Mysqli\Exceptions\OrderByFail
     * @throws \EasySwoole\Mysqli\Exceptions\PrepareQueryFail
     * @throws \Throwable
     */
    public function index(){
        $request = $this->request();
        $page = $request->getRequestParam('page') ?: 1;
        $keyword = $request->getRequestParam('keyword') ?: '';
        $model = new AdminsModel();
        $list = $model->where(function ($query) use($keyword){
            if($keyword){
                $query->where('username','%'.$keyword.'%','like')->whereOr('nickname','%'.$keyword.'%','like');
            }
        })->orderBy('created_at')
        ->paginate($page);
        $this->stateToText($list['data'],[
            'status' => $model::STATUS_TEXT,
        ]);
        return $this->writeJson(0,$list);
    }

    public function update(){

    }

}
